<?php

namespace App\Models\System;

use App\Components\Redis;
use Illuminate\Database\Eloquent\Model;

class SystemUserView extends Model
{
    protected $table = 'system_user_view';

    public static function getView(int $userId)
    {
        $cacheKey = 'MODELS:SYSTEM:SYSTEMUSERVIEW:GET:' . $userId;
        $cacheRs = Redis::get($cacheKey);
        if ($cacheRs !== false && $cacheRs !== null) {
            return json_decode($cacheRs, true);
        }

        $infoRs = self::select('id', 'content')
            ->where('user_id', $userId)->first();
        if (!isset($infoRs->id)) {
            $return = [
                'main_view' => 'light',
                'sidebar_indent' => 'on',
                'sidebar_view' => 'gerneral',
            ];
            Redis::setex($cacheKey, Redis::tinyTime(), json_encode($return));
            return $return;
        }
        Redis::setex($cacheKey, Redis::smallTime(), $infoRs->content);
        return json_decode($infoRs->content, true);
    }

    public static function clearVierCache(int $userId)
    {
        $cacheKey = 'MODELS:SYSTEM:SYSTEMUSERVIEW:GET:' . $userId;
        Redis::del($cacheKey);
        return true;
    }
}
